package com.mingrisoft;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

public class deviceControlActivity extends Activity implements View.OnClickListener{
    private Switch s1, s2, s3, s4, s5;
    private Button editBtnSts,appBtn;
    private int LightLedStatus, warmLedStatus , fanStatus, kettleStatus, shadowStatus;
    mqttManager mqtt_Manager;
    private String sendMsg;
    private String publishTopic="controlDevice";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device_control);
        TextView []tvs={};
        initView();
        mqtt_Manager=mqttManager.getInstance(tvs,0);
        editBtnSts.setOnClickListener(this);
        s1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (s1.isChecked()) {
                    LightLedStatus = 1;
                    Toast.makeText(deviceControlActivity.this, "补光灯开启", Toast.LENGTH_SHORT).show();
                } else {
                    LightLedStatus = 0;
                    Toast.makeText(deviceControlActivity.this, "补光灯关闭", Toast.LENGTH_SHORT).show();
                }
            }
        });
        s2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (s2.isChecked()) {
                    kettleStatus = 1;
                    Toast.makeText(deviceControlActivity.this, "水泵开启", Toast.LENGTH_SHORT).show();
                } else {
                    kettleStatus = 0;
                    Toast.makeText(deviceControlActivity.this, "水泵关闭", Toast.LENGTH_SHORT).show();
                }
            }
        });
        s3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (s3.isChecked()) {
                    shadowStatus = 1;
                    Toast.makeText(deviceControlActivity.this, "舵机开启", Toast.LENGTH_SHORT).show();
                } else {
                    shadowStatus = 0;
                    Toast.makeText(deviceControlActivity.this, "舵机关闭", Toast.LENGTH_SHORT).show();
                }
            }
        });
        s4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (s4.isChecked()) {
                    warmLedStatus = 1;
                    Toast.makeText(deviceControlActivity.this, "暖灯开启", Toast.LENGTH_SHORT).show();
                } else {
                    warmLedStatus = 0;
                    Toast.makeText(deviceControlActivity.this, "暖灯关闭", Toast.LENGTH_SHORT).show();
                }
            }
        });
        s5.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (s5.isChecked()) {
                    fanStatus = 1;
                    Toast.makeText(deviceControlActivity.this, "风扇开启" + fanStatus, Toast.LENGTH_SHORT).show();

                } else {
                    fanStatus = 0;
                    Toast.makeText(deviceControlActivity.this, "风扇关闭", Toast.LENGTH_SHORT).show();

                }
            }
        });
        appBtn.setOnClickListener(this);
    }

    public void onClick(View view){
        int count=0;
        if(view.getId()==R.id.enableSetBtn){
            count+=1;
            s1.setEnabled(true);
            s2.setEnabled(true);
            s3.setEnabled(true);
            s4.setEnabled(true);
            s5.setEnabled(true);
            sendMsg="{"+"\"seq\":"+count+",\"type\":3"+",\"Auto:\"0"+"}";
        }
        else if(view.getId()==R.id.appBtn){

            count+=1;
            sendMsg ="{"+"\"seq\":"+count+",\"type\":3"+",\"LightLedStatus\":"+LightLedStatus+",\"WarmLedStatus\":"+warmLedStatus+",\"FanStatus\":"+fanStatus+",\"KettleStatus\":"+kettleStatus+",\"shadowStatus\":"+shadowStatus+"}";
            mqtt_Manager.publishMessage(publishTopic,sendMsg);
            Toast.makeText(deviceControlActivity.this, sendMsg+"\n", Toast.LENGTH_SHORT).show();
        }
    }

    private void initView() {
        s1 = (Switch) findViewById(R.id.ledSw);
        s2 = (Switch) findViewById(R.id.waterPumpSw);
        s3 = (Switch) findViewById(R.id.servoSw);
        s4 = (Switch) findViewById(R.id.wledSw);
        s5 = (Switch) findViewById(R.id.fanSw);
        editBtnSts=(Button)findViewById(R.id.enableSetBtn);
        appBtn=(Button)findViewById(R.id.appBtn);
    }

}
